home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_500 / swindows / source / swmemory.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  155 lines

  1. /*
  2.  *  SWINDOWS    A program to allow you to open windows on any screen by
  3.  *              supplying the screen name in thw window title
  4.  *
  5.  *              Copyright 1989 by Davide P. Cervone.
  6.  *  You may use this code, provided this copywrite notice is kept intact.
  7.  */
  8.  
  9. #include "swHandler.h"
  10.  
  11.  
  12. SLISTITEM ScreenList = NULL;    /* the linked list of ScreenListItems */
  13. WLISTITEM WindowList = NULL;    /* the linked list of WindowListItems */
  14.  
  15.  
  16. /*
  17.  *  FreeScreenListItem()
  18.  *
  19.  *  Unlink the item from the list, and change the list pointer if
  20.  *  this item was first in the list.
  21.  *  Then free the memory in use by the item.
  22.  */
  23.  
  24. void FreeScreenListItem(theScreen)
  25. SLISTITEM theScreen;
  26. {
  27.    Forbid();
  28.    if (theScreen->Next) theScreen->Next->Prev = theScreen->Prev;
  29.    if (theScreen->Prev) theScreen->Prev->Next = theScreen->Next;
  30.    if (theScreen == ScreenList)    ScreenList = theScreen->Next;
  31.    FREE(ScreenListItem,theScreen);
  32.    Permit();
  33. }
  34.  
  35.  
  36. /*
  37.  *  FreeWindowListItem
  38.  *
  39.  *  Unlink the item from the list, and change the list pointer if
  40.  *  this item was first in the list.  
  41.  *  Then free the memory in use by the item.
  42.  */
  43.  
  44. void FreeWindowListItem(theWindow)
  45. WLISTITEM theWindow;
  46. {
  47.    Forbid();
  48.    if (theWindow->Next) theWindow->Next->Prev = theWindow->Prev;
  49.    if (theWindow->Prev) theWindow->Prev->Next = theWindow->Next;
  50.    if (theWindow == WindowList)    WindowList = theWindow->Next;
  51.    FREE(WindowListItem,theWindow);
  52.    Permit();
  53. }
  54.  
  55.  
  56. /*
  57.  *  NewScreenListItem()
  58.  *
  59.  *  Create a new instance of a ScreenListItem.
  60.  *  If successfull, then
  61.  *    Set the pointer to the Intuition screen.
  62.  *    Add the item into the list.
  63.  *  return the pointer to the new item.
  64.  */
  65.  
  66. static SLISTITEM NewScreenListItem(theScreen)
  67. struct Screen *theScreen;
  68. {
  69.    SLISTITEM ScreenItem;
  70.    
  71.    if (NEW(ScreenListItem,ScreenItem))
  72.    {
  73.       Forbid();
  74.       ScreenItem->Screen = theScreen;
  75.       ScreenItem->Next = ScreenList;
  76.       ScreenItem->Prev = NULL;
  77.       if (ScreenList) ScreenList->Prev = ScreenItem;
  78.       ScreenList = ScreenItem;
  79.       Permit();
  80.    }
  81.    return(ScreenItem);
  82. }
  83.  
  84.  
  85. /*
  86.  *  AddWindowListItem()
  87.  *
  88.  *  For a pre-existing WindowListItem,
  89.  *  Set the window pointer, and add the item into the linked list.
  90.  */
  91.  
  92. void AddWindowListItem(WindowItem,theWindow)
  93. WLISTITEM WindowItem;
  94. struct Window *theWindow;
  95. {
  96.    Forbid();
  97.    WindowItem->Window = theWindow;
  98.    WindowItem->Next = WindowList;
  99.    WindowItem->Prev = NULL;
  100.    if (WindowList) WindowList->Prev = WindowItem;
  101.    WindowList = WindowItem;
  102.    Permit();
  103. }
  104.  
  105.  
  106. /*
  107.  *  FindScreenListItem()
  108.  *
  109.  *  Look through the linked list for the specified screen.
  110.  *  If not found, then try to create a new list item for the screen.
  111.  *  If successful or if it already existed, then increment its use count
  112.  *    (so that the item will not be removed while we are using it)
  113.  *  return the found (or new) screen list item.
  114.  */
  115.  
  116. SLISTITEM FindScreenListItem(theScreen)
  117. struct Screen *theScreen;
  118. {
  119.    SLISTITEM ScreenItem;
  120.    
  121.    Forbid();
  122.    ScreenItem = ScreenList;
  123.    while (ScreenItem && ScreenItem->Screen != theScreen)
  124.       ScreenItem = ScreenItem->Next;
  125.    if (ScreenItem == NULL) ScreenItem = NewScreenListItem(theScreen);
  126.    if (ScreenItem) ScreenItem->UseCount++;
  127.    Permit();
  128.    return(ScreenItem);
  129. }
  130.  
  131.  
  132. /*
  133.  *  UnuseScreenListItem()
  134.  *
  135.  *  Decement the use count for the item.
  136.  *  If this was the last use, then
  137.  *    If a task is waiting to close this screen,
  138.  *      signal that it is now OK to close the screen.
  139.  *    Free the memory in use by the screen item.
  140.  */
  141.  
  142. void UnuseScreenListItem(ScreenItem)
  143. SLISTITEM ScreenItem;
  144. {
  145.    Forbid();
  146.    ScreenItem->UseCount--;
  147.    if (ScreenItem->UseCount <= 0)
  148.    {
  149.       if (ScreenItem->CloseTask)
  150.          Signal(ScreenItem->CloseTask,ScreenItem->CloseSignal);
  151.       FreeScreenListItem(ScreenItem);
  152.    }
  153.    Permit();
  154. }
  155.